# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render

from social.models import *

from django.http import Http404
from django.shortcuts import redirect
from django.contrib.auth import authenticate

from django.shortcuts import get_object_or_404, render
from django.utils import simplejson
from django.db.models import Q

def isset(dict, string):
    try:
        if dict[string]:
            return True
    except:
        return False


def index(request):
    try:
        if isset(request.session, 'user_id') and isset(request.session, 'first_name') and isset(request.session, 'last_name'):
            template = loader.get_template('social/profile.html')
            context = RequestContext(request, )
            return HttpResponse(template.render(context))
        try:
            user = User.objects.create(firstName=request.POST['firstname'],
                lastName=request.POST['lastname'],
                password=request.POST['password'],
                email=request.POST['email'],
            )
            user.save()
            request.session['user_id'] = user.id
            request.session['first_name'] = user.firstName
            request.session['last_name'] = user.lastName

            template = loader.get_template('social/profile.html')
            context = RequestContext(request, )
            return HttpResponse(template.render(context))
        except:

            loginedUser = User.objects.filter(email=request.POST['email'])
            if loginedUser:
                if loginedUser[0].password == request.POST['password']:
                    request.session['user_id'] = loginedUser[0].id
                    request.session['first_name'] = loginedUser[0].firstName
                    request.session['last_name'] = loginedUser[0].lastName
                    # request.session['posts'] = loginedUser[0].createdPosts.all()
                    template = loader.get_template('social/profile.html')
                    context = RequestContext(request, )
                    return HttpResponse(template.render(context))
                else:
                    template = loader.get_template('social/index.html')
                    context = RequestContext(request, {
                        'error': 'your password is not correct !!'
                    })
                    return HttpResponse(template.render(context))
            else:
                template = loader.get_template('social/index.html')
                context = RequestContext(request, {
                    'error': "this email doesn't exist !!"
                })
                return HttpResponse(template.render(context))


    except:
        return render(request, 'social/index.html')



def nameDetailIndex(request, first_name, last_name, queueNumber=None):
    try:
        user = User.objects.filter(firstName=first_name, lastName=last_name)
        if user:
            if not queueNumber:
                if len(user) > 1:
                    user = user[0]
            else:
                #start id counter from 1 not 0 because style of URL :D

                user = user[int(queueNumber)-1]
        else:
            raise Http404
        return render(request, 'social/public.html', {
            'user': user
        })
    except:
        raise Http404


def idDetailIndex(request, user_id):
    user = get_object_or_404(User, id=user_id)
    return redirect('FLSocial', first_name=user.firstName, last_name=user.lastName)


